// pages/microStation/brand.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.loadData()
    },
    loadData() {
        wx.showLoading({
            title: '加载中...',
        })
        app.http.get('city/tiny-station/list-all-brands').then(res => {
            this.setData({
                brand: res.data.allBrand,
                myBrand: res.data.myBrand
            })
            wx.hideLoading()
        })
    },
    choiceBrand(e) {
        var index = e.currentTarget.dataset.index
        let choice = this.data.brand[index]
        if (this.data.myBrand.length >= 5) {
            wx.showToast({
                title: '最多五个品牌',
                icon: 'none'
            })
            return
        }
        console.log(choice, 'choice')
        this.setData({
            myBrand: this.data.myBrand.concat(choice),
        })
    },
    searchChoiceList(e) {
        var index = e.currentTarget.dataset.index
        let choice = this.data.searchList[index]
        if (this.data.myBrand.length >= 5) {
            wx.showToast({
                title: '最多五个品牌',
                icon: 'none'
            })
            return
        }
        this.setData({
            myBrand: this.data.myBrand.concat(choice),
        })
    },
    deleteBrand(e) {
        console.log(e)
        var id = e.currentTarget.dataset.id
        var index = e.currentTarget.dataset.index

        var del = this.data.myBrand.splice(index, 1)
        console.log(id, 'idsss')
        app.http.get('city/tiny-station/del-brand', {
            data: {
                brand_id: id
            }
        }).then(res => {
            this.setData({
                myBrand: this.data.myBrand.slice(del),
            })
        })
    },
    addBrand() {
        wx.showLoading({
            title: '加载中...',
        })
        app.http.get('city/tiny-station/choose-brand', {
            data: {
                brand_id: this.data.myBrand
            }
        }).then(res => {
            setTimeout(() => {
                wx.showToast({
                    title: '保存成功！',
                    icon: 'none',
                    success: res => {
                        wx.navigateBack()

                    }
                })

            }, 1000)
            wx.hideLoading()
        })
    },

    searchBrand(e) {
        this.setData({
            content: e.detail.value
        })
        if (!e.detail.value) {
            return
        }
        app.http.get('city/tiny-station/search-brand', {
            data: {
                name: e.detail.value
            }
        }).then(res => {
            this.setData({
                searchList: res.data
            })
        })
    }
})